#!/bin/bash

RANK_SIZE=$1
DEVICE_START=$2
DATA_PATH=/home/licheng/dataset/cifar10/train/
export DATA_PATH=${DATA_PATH}
EXEC_PATH=$(pwd)

echo "====check values===="
echo RANK_SIZE=$RANK_SIZE
echo DEVICE_START=$DEVICE_START
echo DATA_PATH=$DATA_PATH
echo EXEC_PATH=$EXEC_PATH
echo "====check done===="


function test_dist_pcs(){
    export MINDSPORE_HCCL_CONFIG_PATH=${EXEC_PATH}/rank_table_$1pcs.json
    export RANK_SIZE=$1
}
test_dist_pcs ${RANK_SIZE}

for((i=(($RANK_SIZE - 1)); i>=0; i--))
do
    rm -rf device$i
    mkdir device$i
    cp ./*.py ./device$i
    cd ./device$i
    DEVICE_ID=$[i+DEVICE_START]
    export DEVICE_ID=$DEVICE_ID
    export RANK_ID=$i
    echo "start training for rank $RANK_ID device $DEVICE_ID"
    env > env$i.log
    if (($i == 0))
    then
        pytest -s -v ./main.py > train.log$i 2>&1
    else
        pytest -s -v ./main.py > train.log$i 2>&1 &
    fi
    cd ../
done
